<?php

namespace App\Models;

class Demands extends Base
{
    protected $table = 'demands';

 
    const STATUS_1 = 1;
    const STATUS_2 = 2;
    const STATUS_3 = 3;
    const STATUS_4 = 4;
    const STATUS_5 = 5;
    
    const STATUS_TEXT = [
        self::STATUS_1 => '<span class="el-tag el-tag--info">待审核</span>',
        self::STATUS_2 => '<span class="el-tag el-tag--danger">已驳回</span>',
        self::STATUS_3 => '<span class="el-tag ">进行中</span>',
    	self::STATUS_4 => '<span class="el-tag el-tag--danger">强制结束</span>', 
    	self::STATUS_5 => '<span class="el-tag el-tag--danger">已完成</span>',
   ];
    
    

    protected $fillable = [
        'id','user_id','user_name','title',
    	'cate_pid','cate_id',
    	'pttype_id','content',
    	'area','price','mission_limit',
        'number','ownnumber','view','start_at',
        'end_at','type','status','r_status',
    	'created_at','updated_at','unit',
    	'step1_desc','step1_image',
    	'step2_desc','step2_image',
    	'step3_desc','step3_image',
    	'step4_desc','step4_image',
    	'step5_desc','step5_image',
    	'step6_desc','step6_image',
    	'is_recommend'
    ];

    public function cate_pid(){//一级分类
        return $this->belongsTo('App\Models\Categorys', 'cate_pid', 'id');
    }
    public function cate_id(){//二级分类
        return $this->belongsTo('App\Models\Categorys', 'cate_id', 'id');
    }
    public function pttype_id(){//任务类别
    	return $this->belongsTo('App\Models\rewardTypes', 'pttype_id', 'id');
    }
    public function area(){//区域
        return $this->belongsTo('App\Models\Areas', 'area', 'id');
    }
	
    /**
     * 获取列表
     * @param array $params 条件参数
     * @param array $fields 查询的字段
     * @param int $limit 分页数量
     * @return mixed
     */
    protected function getList($params = [], $fields = ['*'], $limit = 5){
        $lists = $this->select($fields)
            ->where(function ($query) use ($params) {
                if(isset($params['kwd']) && $params['kwd']) {
                    $query ->where('title','like','%' . $params['kwd'] . '%');
                }
                if(isset($params['status']) && $params['status']) {
                    $query ->where('status',$params['status']);
                }
                
                if(isset($params['r_status']) && $params['r_status']) {
                	$query ->where('r_status',$params['r_status']);
                }
                
                if(isset($params['cid']) && $params['cid']) {
                    $query ->where('cate_id',$params['cid'])->orwhere('cate_pid',$params['cid']);
                }
                
                if(isset($params['tid']) && $params['tid']) {
                	$query ->where('pttype_id',$params['tid']);
                }
                
                if(isset($params['start']) && $params['start']) {
                    $query ->whereDate('created_at','>=',$params['start']);
                }
                if(isset($params['end']) && $params['end']) {
                    $query ->whereDate('created_at','<=',$params['end']);
                }
               
            })->where('status','<>',-1)
            ->with(['cate_id' => function ($query) {
                $query->select(['id','name'])->get();
            }])->with(['cate_pid' => function ($query) {
                $query->select(['id','name'])->get();
            }])->with(['pttype_id' => function ($query) {
                $query->select(['id','name','before_icon'])->get();
            }])->when($params, function ($query) use($params){
                if(isset($params['sort']) && $params['sort']) {
                    return $query->orderBy('price',$params['sort']);
                }
            })->orderBy('created_at', 'desc')->orderBy('end_at', 'asc')
            ->paginate($limit);
	
        $this->stateToText($lists,[
            'status'=> self::STATUS_TEXT
        ]);

        return $lists;
    }

    /**
     * 所有数据
     * @param array $params 条件参数
     * @param array $fields 查询的字段
     * @param int $limit 分页数量
     * @return mixed
     */
    protected function allLists($params = [], $fields = ['*'], $page = null, $limit = 10, $sort='', $sort_by='desc'){

       // var_dump($sort." ".$sort_by);
    	if(isset($page)){
            $lists = $this->select($fields)
                ->where(function ($query) use ($params) {
                    if(isset($params['status']) && $params['status']) {
                        $query ->where('status',$params['status']);
                    }else{
                    	$query ->where('status',3);
                    }
                    if(isset($params['user_id']) && $params['user_id']>0) {
                        $query ->where('user_id',$params['user_id']);
                    }
                    if(isset($params['area']) && $params['area']) {
                        $query ->where('area',$params['area']);
                    }                                                           
                    if(isset($params['cid']) && $params['cid']) {
                        $query ->where('cate_pid',$params['cid']);
                    }
                    if(isset($params['cid_id']) && $params['cid_id']) {
                        $query ->where('cate_id',$params['cid_id']);
                    }

                    if(isset($params['pttype_id']) && $params['pttype_id']) {
                    	$query ->where('pttype_id',$params['pttype_id']);
                    }
                    
                    if(isset($params['keywords']) && $params['keywords']) {
                        $keyword = $params['keywords'];
                        $query ->where("title", "like", "%$keyword%");
                    }
                    if(isset($params['show']) && $params['show']){
                    	$query ->whereDate('end_at','>=',date('Y-m-d H:i:s',time()));
                    }
                    
                })->with(['area' => function ($query) {
			                $query->select(['id','name'])->get();
			            }])
		          ->with(['cate_id' => function ($query) {
		                $query->select(['id','name'])->get();
		            }])->with(['cate_pid' => function ($query) {
		                $query->select(['id','name'])->get();
		            }])->with(['pttype_id' => function ($query) {
		                $query->select(['id','name','before_icon'])->get();
		            }])->orderBy('is_recommend','desc')->orderBy($sort,$sort_by)->orderBy('end_at', 'asc')->paginate($limit, null, null, $page);
        }else{
            $lists = $this->select($fields)
                ->where(function ($query) use ($params) {
                    if(isset($params['status']) && $params['status']) {
                        $query ->where('status',$params['status']);
                    }else{
                    	 $query ->where('status',3);
                    }
                    if(isset($params['user_id']) && $params['user_id']) {
                        $query ->where('user_id',$params['user_id']);
                    }
                    if(isset($params['area']) && $params['area']) {
                        $query ->where('area',$params['area']);
                    }
                    
                    
                    if(isset($params['cid']) && $params['cid']) {
                        $query ->where('cate_pid',$params['cid']);
                    }
                    if(isset($params['cid_id']) && $params['cid_id']) {
                        $query ->where('cate_id',$params['cid_id']);
                    }
                    
                    if(isset($params['start_time']) && $params['start_time']) {
                        $query ->whereDate('end_at','>=',$params['start_time']);
                    }
                    if(isset($params['end_time']) && $params['end_time']) {
                        $query ->whereDate('end_at','<=',$params['end_time']);
                    }
                    if(isset($params['keyword']) && $params['keyword']) {
                        $keyword = $params['keyword'];
                        $query ->where("title", "like", "%$keyword%");
                    }
                	if(isset($params['show']) && $params['show']){
                    	$query ->whereDate('end_at','>=',date('Y-m-d H:i:s',time()));
                    }
                })->with(['area' => function ($query) {
			                $query->select(['id','name','before_icon'])->get();
			            }])
		          ->with(['cate_id' => function ($query) {
		                $query->select(['id','name'])->get();
		            }])->with(['cate_pid' => function ($query) {
		                $query->select(['id','name'])->get();
		            }])->with(['pttype_id' => function ($query) {
		                $query->select(['id','name'])->get();
		            }])->orderBy('is_recommend','desc')->orderBy($sort,$sort_by)->orderBy('end_at', 'asc')->limit($limit)->get();
		            
        }

        $this->stateToText($lists,[
        	'status'=> self::STATUS_TEXT
        		
        ]);
         
        return $lists;
    }

    /**
     * 根据id查询一条信息
     * @param array $params 条件参数
     * @param array $fields 查询的字段
     * @return mixed
     */
    protected function details($params = [], $fields = ['*']){

        return $this->select($fields)->where($params)->where('status','<>',-1)
            ->with(['area' => function ($query) {
			                $query->select(['id','name'])->get();
			            }])
		          ->with(['cate_id' => function ($query) {
		                $query->select(['id','name'])->get();
		            }])->with(['cate_pid' => function ($query) {
		                $query->select(['id','name'])->get();
		            }])->with(['pttype_id' => function ($query) {
		                $query->select(['id','name','before_icon'])->get();
		            }])->first();
    }
   
    
    /**
     * 获取导出数据
     * @param array $params 条件参数
     * @return mixed
     */
    protected function exports($params){
    	//'任务分类','任务金额','任务总数','任务完成个数','发布人','任务状态'
    	$fields = [
    		'title','pttype_id','price','number','ownnumber','user_name','status'
    	];
    	$lists = $this->select($fields)
    	->where(function ($query) use ($params) {
    		if(isset($params) && $params) {
    			$ids = explode('-',$params);
    			$query ->whereIn('id',$ids);
    		}
    	})->where('status','<>',-1)->get();
    
    	foreach ($lists as $k=>$v){

    		$v->attributes['pttype_id'] = rewardTypes::where("id",$v->attributes['pttype_id'])->value("name");
    		
    		if($v->attributes['status'] == 1){
    			$v->attributes['status'] ="待审核";
	    	}elseif($v->attributes['status'] == 2){
	    		$v->attributes['status'] ="已驳回";
	    	}elseif($v->attributes['status'] == 3){
	    		$v->attributes['status'] ="进行中";
	    	}elseif($v->attributes['status'] == 4){
	    		$v->attributes['status'] ="强制结束";
	    	}else{
	    		$v->attributes['status'] ="已完结";
		    }
		    
		}
	    return $lists;
	 }
}
